package com.pdsu.xpf.graduation01.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.xpf.graduation01.domain.Agriculture;
import com.pdsu.xpf.graduation01.domain.Commodity;
import com.pdsu.xpf.graduation01.domain.User;
import com.pdsu.xpf.graduation01.service.impl.AgricultureServiceImpl;
import com.pdsu.xpf.graduation01.service.impl.UserServiceImpl;
import com.pdsu.xpf.graduation01.utils.JsonFormat;
import com.pdsu.xpf.graduation01.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/agriculture")
public class AgricultureControl {

    final
    UserServiceImpl userService;

    final
    AgricultureServiceImpl agricultureService;

    public AgricultureControl(AgricultureServiceImpl agricultureService, UserServiceImpl userService) {
        this.agricultureService = agricultureService;
        this.userService = userService;
    }

    @ResponseBody
    @RequestMapping("/getAllAgriculture")
    public Result getAllAgriculture(HttpServletRequest request) throws JsonProcessingException {


        List<Agriculture> agricultures= agricultureService.findAllAgriculture();

        String a_json= JsonFormat.ListToJson(agricultures);
        System.out.println("----"+a_json);

        return Result.success(a_json);

    }
    //findAgricultureByAID
    @ResponseBody
    @RequestMapping("/findAgricultureByAID")
    public Result findAgricultureByAID(HttpServletRequest request,int aid) throws JsonProcessingException {

        System.out.println(aid);


        Agriculture agriculture= agricultureService.findAgricultureByAID(aid);

        String a_json=JsonFormat.ObjToJson(agriculture);
        System.out.println("----"+a_json);
        return Result.success(a_json);

    }

    @ResponseBody
    @RequestMapping("/findAgricultureUserByUID")
    public Result findAgricultureUserByUID(HttpServletRequest request,int uid) throws JsonProcessingException {



       User user= userService.findUserByUID(uid);

        return Result.success(user.getUsername());

    }


}
